package com.xiaolin.controller;

import com.xiaolin.pojo.Message;
import com.xiaolin.pojo.vo.BaseResult;
import com.xiaolin.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author XiaoLin
 * @date 2019/2/25 09:11
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    /**
     * 查询登陆用户最近聊天
     *
     * @param request
     * @return
     */
    @GetMapping("/findMessage")
    public ResponseEntity<BaseResult> findMessage(HttpServletRequest request) {
        try {
            //获取用户Id
            String userId = (String) request.getAttribute("userId");
            //查询用户好友信息
            List<Message> friendsList = messageService.findMessageByUId(userId);
            return ResponseEntity.ok(new BaseResult(0, "最近聊天获取成功!", friendsList));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(new BaseResult(1, "最近聊天获取失败!"));
    }

}
